﻿// =================================================================== 
// 项目说明
//====================================================================
// lsc @Copy Right 2019
// 文件： UsersRepository.cs
// 项目名称：斑马车务
// 创建时间：2019-12-11
// 负责人：lsc
// ===================================================================
using System;
using System.Text;
using System.Collections.Generic;
using CRM.Infrastructure.Entity;
using CRM.Infrastructure.Models;
using Dapper;
using Dapper.Extensions;

namespace CRM.Infrastructure.Repository
{
    /// <summary>
    ///用户表 仓储层
    /// </summary>	
    public class UsersRepository : BaseRepository<Users>
    {

        #region 构造

        public UsersRepository() : base() { }

        public UsersRepository(string connectionString) : base(connectionString) { }

        public UsersRepository(DatabaseContext dbContext) : base(dbContext) { }

        #endregion

        public Users GetByLoginNum(string userName)
        {
            string sql = "SELECT * FROM Users WITH(NOLOCK) WHERE IsActive=1 AND UserName=@UserName";
            return FirstOrDefault(sql, new { UserName = userName });
        }
        /// <summary>
        /// 查询用户列表
        /// 此方法仅用于管理员
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public Page<Users> Pages(int pageIndex, int pageSize, UserSearchModel model)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("SELECT T1.* FROM [Users] T1 WITH(NOLOCK) ");
            sb.Append(" WHERE T1.IsActive=1");
            var dparams = new DynamicParameters();
            if (model != null)
            {
                AddParams(model, sb, dparams);
            }
            sb.Append(" ORDER BY T1.UserID DESC");
            return dbContext.Connection.Page<Users>(pageIndex, pageSize, sb.ToString(), dparams);
        }
        /// <summary>
        /// 构建查询条件
        /// </summary>
        /// <param name="model"></param>
        /// <param name="sb"></param>
        /// <param name="dparams"></param>
        private void AddParams(UserSearchModel model, StringBuilder sb, DynamicParameters dparams)
        {
            if (!string.IsNullOrEmpty(model.UserMobile))
            {
                sb.Append(" AND T1.Mobile=@Mobile");
                dparams.Add("Mobile", model.UserMobile, System.Data.DbType.String);
            }

            if (model.CompanyID > -1)
            {
                sb.Append(" AND T1.CompanyID=@CompanyID");
                dparams.Add("CompanyID", model.CompanyID, System.Data.DbType.Int32);
            }

            if (!string.IsNullOrWhiteSpace(model.UserName))
            {
                sb.Append(" AND charindex(@UserName,T1.UserName)>0");
                dparams.Add("UserName", model.UserName, System.Data.DbType.String);
            }
            if (model.RoleID > 0)
            {
                sb.Append(" AND T1.RoleID=@RoleID");
                dparams.Add("RoleID", model.RoleID, System.Data.DbType.Int32);
            }
        }

        /// <summary>
        /// 设置用户逻辑删除
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="isActvie"></param>
        public void SetIsActive(long userId, int isActvie)
        {
            string sql = "UPDATE Users SET IsActive={0} WHERE UserID={1}";
            Execute(string.Format(sql, isActvie, userId));
        }

        public List<Users> GetByCompany(int companyId)
        {
            string sql = "SELECT * FROM Users WHERE IsActive=1 AND IsStop=0 AND CompanyID=" + companyId;
            return Query(sql);
        }
    }
}